package OperationSystem.day1203blocking_queue_阻塞队列;

import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class One_Producer_With_One_Consumer {//单生产者与单消费者模式
    private static CircleQueue queue=new CircleQueue();

    private static class Producers extends Thread{
        Scanner scanner=new Scanner(System.in);
        @Override
        public void run() {
            try {
                while (true){
                    System.out.println("请输入下一个数字:");
                    int element=scanner.nextInt();
                    queue.put(element);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    private static class Consumers extends Thread{
        @Override
        public void run() {
            Random random=new Random(20201203);
            try {
                while (true){
                    int element=queue.take();
                    System.out.println("消费者读出:"+element);
                    TimeUnit.SECONDS.sleep(10);//最多等10s
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        Producers p=new Producers();
        p.start();
        Consumers c=new Consumers();
        c.start();
    }
}
